package cn.lili.modules.order.cart.render.impl;

import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.common.utils.SpringContextUtil;
import cn.lili.modules.distribution.client.DistributionGoodsClient;
import cn.lili.modules.distribution.entity.dos.Distribution;
import cn.lili.modules.distribution.entity.dos.DistributionGoods;
import cn.lili.modules.order.cart.entity.dto.TradeDTO;
import cn.lili.modules.order.cart.entity.enums.RenderStepEnums;
import cn.lili.modules.order.cart.entity.vo.CartSkuVO;
import cn.lili.modules.order.cart.render.CartRenderStep;
import cn.lili.modules.system.client.SettingClient;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.entity.enums.SettingEnum;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分销佣金计算
 *
 * @author Chopper
 * @since 2020-07-02 14:47
 */
@Service
@RequiredArgsConstructor
public class DistributionPriceRender implements CartRenderStep {
    /**
     * 缓存
     */
    private final Cache cache;

    private final DistributionGoodsClient distributionGoodsClient;

    @Override
    public RenderStepEnums step() {
        return RenderStepEnums.DISTRIBUTION;
    }

    @Override
    public void render(TradeDTO tradeDTO) {
        this.renderDistribution(tradeDTO);
    }

    /**
     * 渲染分销佣金
     *
     * @param tradeDTO 购物车信息
     */
    private void renderDistribution(TradeDTO tradeDTO) {

//        //如果存在分销员
//        Distribution distribution = (Distribution) cache.get(CachePrefix.DISTRIBUTION.getPrefix() + tradeDTO.getDistributionId());
//        if (distribution == null) {
//            return;
//        }
//        //循环订单商品列表，如果是分销商品则计算商品佣金
//        tradeDTO.setDistributionId(distribution.getMemberId());
//        tradeDTO.setDistributionName(distribution.getMemberName());
//
//        List<String> skuIds = tradeDTO.getCheckedSkuList().stream().map(cartSkuVO -> cartSkuVO.getGoodsSku().getId()).toList();
//        //是否包含分销商品
//        List<DistributionGoods> distributionGoods = distributionGoodsClient.distributionGoods(skuIds);
//        if (distributionGoods != null && !distributionGoods.isEmpty()) {
//            distributionGoods.forEach(dg -> tradeDTO.getCheckedSkuList().forEach(cartSkuVO -> {
//                if (cartSkuVO.getGoodsSku().getId().equals(dg.getSkuId())) {
//                    cartSkuVO.setDistributionGoods(dg);
//                }
//            }));
//        }
//
////        for (CartSkuVO cartSkuVO : tradeDTO.getCheckedSkuList()) {
////            if (cartSkuVO.getDistributionGoods() != null) {
////                cartSkuVO.getPriceDetailDTO().setDistributorSettlementPrice(CurrencyUtil.mul(cartSkuVO.getNum(),
////                        cartSkuVO.getDistributionGoods().getCommission()));
////            }
////        }
//
//        SettingClient settingClient = SpringContextUtil.getBean(SettingClient.class);
//        Setting settingGreenGoods = settingClient.get(SettingEnum.GREEN_GOODS.name());
//        Setting settingNotGreenGoods = settingClient.get(SettingEnum.NOT_GREEN_GOODS.name());
//
//        for (CartSkuVO cartSkuVO : tradeDTO.getCheckedSkuList()) {
//            if (cartSkuVO.getDistributionGoods() != null) {
//
//                Double rate = 0.0;
//                if(cartSkuVO.getIsGreenGoods()) {
//                    rate = CurrencyUtil.div(JSONObject.parseObject(settingGreenGoods.getSettingValue()).getDoubleValue("distributionProportion"), 100);
//                }else {
//                    rate = CurrencyUtil.div(JSONObject.parseObject(settingNotGreenGoods.getSettingValue()).getDoubleValue("distributionProportion"), 100);
//                }
//
//                cartSkuVO.getPriceDetailDTO().setDistributorSettlementPrice(CurrencyUtil.mul(cartSkuVO.getNum(),
//                        CurrencyUtil.mul(cartSkuVO.getPriceDetailDTO().getGreenScore(), rate)));
//            }
//        }

    }
}
